package com.dynamic.model.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.dynamic.model.common.vo.Page;
import com.dynamic.model.dao.SystemResourceDao;
import com.dynamic.model.common.dao.impl.BaseDaoImpl;
import com.dynamic.model.model.po.SysmanResource;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import com.dynamic.model.common.entity.BaseEntity;

/**
 * 
 * @desc 用户后台资源资源权限表--RBAC权限管理dao接口实现类
 *
 * @author luwei
 * @Fri Aug 08 13:29:41 CST 2014
 */
@Repository
public class SystemResourceDaoImpl extends BaseDaoImpl<SysmanResource> implements SystemResourceDao {

	@Override
	public List<SysmanResource> getRootResourceList() {
		String hql = "from " + this.clazz.getName() + "  where deleteFlag=0  and parent is null ";
		return this.getSessionFactory().getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<SysmanResource> getPageDataReqName(Page<SysmanResource> page, String menuname, String type) {
		List<Object> prams=new ArrayList<Object>();
		String hql="from SysmanResource t where t.deleteFlag = 0 ";
		if(null!=menuname&&menuname.length()>0){
			hql=hql+" and t.name=?";
			prams.add(menuname);
		}
		if(null!=type&&type.length()>0){
			hql=hql+" and t.resourceType=?";
			prams.add(Integer.parseInt(type));
		}
		hql=hql+" order by t.createTime desc,pid desc";
		return this.queryForList(hql, prams, page );
	}
	
	@Override
	public void deleteLogicByIds(String[] pidArray) throws Exception {
		String hql ="update  "+this.clazz.getName()+" t set   t.deleteFlag = ?  where t.pid in ( :pids ) or t.parentid in ( :pids )" ;
		
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		
		query.setInteger(0, BaseEntity.DELETE_FLAG_YES);
		
		List<Integer> _tempList = new ArrayList<Integer>();
		for(int i = 0; i < pidArray.length ; i++){
			_tempList.add(Integer.parseInt(pidArray[i]));
		}
		
		query.setParameterList("pids", _tempList);
		
		query.executeUpdate();
	}
}
